package com.syh.jn.scm.service.impl.supplier.base.goods;

import cn.redscarf.commons.util.other.ExtAssert;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.base.goods.GoodsBrand;
import com.syh.jn.scm.domain.supplier.repository.base.goods.GoodsBrandRepository;
import com.syh.jn.scm.dto.supplier.base.goods.goodsBrand.GoodsBrandMto;
import com.syh.jn.scm.dto.supplier.base.goods.goodsBrand.GoodsBrandSto;
import com.syh.jn.scm.dto.supplier.base.goods.goodsBrand.GoodsBrandVo;
import com.syh.jn.scm.service.api.supplier.base.goods.GoodsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class GoodsBrandServiceImpl extends AbstractServiceImpl<GoodsBrandSto, GoodsBrandMto, GoodsBrandVo, GoodsBrand, Long> implements GoodsBrandService {
  @Autowired
  private GoodsBrandRepository goodsBrandRepository;

  @Override
  protected AggregateRepository<GoodsBrand, Long> getAggregateRepository() {
    return goodsBrandRepository;
  }

  /**
   * 新增商品品牌
   *
   * @param sto
   * @return
   */
  @Override
  public Long save(GoodsBrandSto sto) {
    GoodsBrand goodsBrand = new GoodsBrand(sto);
    goodsBrand.enabled();
    return goodsBrandRepository.add(goodsBrand);
  }

  /**
   * 修改商品品牌
   *
   * @param mto
   */
  @Override
  public void update(GoodsBrandMto mto) {
    GoodsBrand goodsBrand = idIsNull(mto.getId());
    goodsBrand.modify(mto);
    goodsBrandRepository.update(goodsBrand);
  }

  /**
   * 根据id删除商品品牌
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    GoodsBrand goodsBrand = idIsNull(id);
    goodsBrand.destroy();
    goodsBrandRepository.remove(goodsBrand);
  }

  /**
   * 对id进行判空
   */
  private GoodsBrand idIsNull(Long id){
    ExtAssert.notNull(id,"id不能为空");
    GoodsBrand goodsBrand =goodsBrandRepository.getById(id);
    ExtAssert.notNull(goodsBrand,"数据不存在");
    return goodsBrand;
  }

  /**
   * 启用
   *
   * @param id
   */
  @Override
  public void enabled(Long id) {
    GoodsBrand goodsBrand = idIsNull(id);
    goodsBrand.enabled();
    goodsBrandRepository.update(goodsBrand);
  }

  /**
   * 停用
   *
   * @param id
   */
  @Override
  public void disabled(Long id) {
    GoodsBrand goodsBrand = idIsNull(id);
    goodsBrand.disabled();
    goodsBrandRepository.update(goodsBrand);
  }
}
